#include "usart.h"
#include "mcu_api.h"
#include "wifi.h"

void USART1_Init(void)
{
		GPIO_Config_T gpioConfig;
    USART_Config_T usartConfigStruct;
		
		RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOB);               //打开时钟
		RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_USART1);
	
		GPIO_ConfigPinAF(GPIOB,GPIO_PIN_SOURCE_6,GPIO_AF_PIN0);    //更改PB6引脚映射功能
		GPIO_ConfigPinAF(GPIOB,GPIO_PIN_SOURCE_7,GPIO_AF_PIN0);
	
		gpioConfig.pin = GPIO_PIN_6;
		gpioConfig.mode = GPIO_MODE_AF;                           //配置为备用功能模式   
    gpioConfig.speed = GPIO_SPEED_50MHz;
    gpioConfig.outtype = GPIO_OUT_TYPE_PP;            
    gpioConfig.pupd = GPIO_PUPD_PU;
    GPIO_Config(GPIOB, &gpioConfig);
	
   	gpioConfig.pin  = GPIO_PIN_7;                   //配置为浮空
	 	GPIO_Config(GPIOB, &gpioConfig);

		usartConfigStruct.baudRate = 9600;
    usartConfigStruct.mode     = USART_MODE_TX_RX;  
    usartConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;  
    usartConfigStruct.parity   = USART_PARITY_NONE;   
    usartConfigStruct.stopBits =  USART_STOP_BIT_1;   
    usartConfigStruct.wordLength = USART_WORD_LEN_8B;    
    USART_Config(USART1, &usartConfigStruct);
		
		
    USART_EnableInterrupt(USART1, USART_INT_RXBNEIE);
    NVIC_EnableIRQRequest(USART1_IRQn, 2);
		
    /* Enable USART */
    USART_Enable(USART1);
		USART_ClearIntFlag(USART1,USART_FLAG_RXBNE);


}

void USART2_Init(void)
{
			GPIO_Config_T GPIO_ConfigStruct;
    USART_Config_T USART_ConfigStruct;

    /* System Clocks Configuration */
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOA);
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_USART2);

    /* Configure Smartcard USART Tx as alternate function open-drain */
		GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_2, GPIO_AF_PIN1);
		GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_3, GPIO_AF_PIN1);
    GPIO_ConfigStruct.pin = GPIO_PIN_2;
    GPIO_ConfigStruct.mode = GPIO_MODE_AF;
    GPIO_ConfigStruct.outtype = GPIO_OUT_TYPE_PP;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_ConfigStruct.pupd = GPIO_PUPD_PU;
    GPIO_Config(GPIOA, &GPIO_ConfigStruct);
	
		GPIO_ConfigStruct.pin = GPIO_PIN_3;
    GPIO_Config(GPIOA, &GPIO_ConfigStruct);

    USART_ConfigStruct.baudRate = 9600;
    USART_ConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
    USART_ConfigStruct.mode = USART_MODE_TX_RX;
    USART_ConfigStruct.parity = USART_PARITY_NONE;
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;

    USART_Config(USART2, &USART_ConfigStruct);

    /* Configure the USART (USART2) Interrupt */
//    USART_EnableInterrupt(USART2, USART_INT_RXBNEIE);
//    NVIC_EnableIRQRequest(USART2_IRQn, 2);

    /* Enable the USART (USART2) */
    USART_Enable(USART2);
		USART_ClearIntFlag(USART2,USART_FLAG_RXBNE);
}



/***************串口1发送函数********************/

void USART_Write(int dat)
{
			while (USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET)
			;
			USART_TxData(USART1, (uint8_t)dat);
}

/*!
 * @brief ： USART_Receive_Isr
 *
 * @param ：串口1接受中断函数
 *
 * @retval ：无
 *
 * @note ：无
 */
void USART_Receive_Isr(void)
{
    uint8_t dat;
	if (USART_ReadStatusFlag(USART1, USART_FLAG_RXBNE) == SET)
    {
        dat = (uint8_t)USART_RxData(USART1);
				uart_receive_input(dat);
			USART_ClearIntFlag(USART1,USART_FLAG_RXBNE);
//		printf("%x",dat);
    }
}




/***************串口2打印函数，用来调试***************/
int fputc(int ch, FILE* f)
{
    USART_TxData(USART2, (uint8_t)ch);
    while (USART_ReadStatusFlag(USART2, USART_FLAG_TXBE) == RESET);
    return (ch);
}



